package com.sakiko.audiobook.user.strategy.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sakiko.audiobook.album.TrackInfoFeignClient;
import com.sakiko.audiobook.common.result.Result;
import com.sakiko.audiobook.model.album.TrackInfo;
import com.sakiko.audiobook.model.user.UserPaidTrack;
import com.sakiko.audiobook.user.mapper.UserPaidTrackMapper;
import com.sakiko.audiobook.user.strategy.ItemTypeStrategy;
import com.sakiko.audiobook.vo.user.UserPaidRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: sakiko
 * time: 2025/11/9 0:26
 * description: 针对 声音订单的处理
 */
@Service("1002")
public class TrackStrategy implements ItemTypeStrategy {

    @Qualifier("com.sakiko.audiobook.album.TrackInfoFeignClient")
    @Autowired
    private TrackInfoFeignClient trackInfoFeignClient;

    @Autowired
    private UserPaidTrackMapper userPaidTrackMapper;

    @Override
    public void savePaidRecord(UserPaidRecordVo userPaidRecordVo) {
        // 购买声音
        List<Long> trackIdList = userPaidRecordVo.getItemIdList();
//            UserPaidTrack userPaidTrack = userPaidTrackMapper.selectById(trackIdList.get(0)); 不可能存在要插入的记录 这里绝对的错误 ❌
        Result<TrackInfo> trackInfoResult = trackInfoFeignClient.getTrackInfo(trackIdList.get(0));
        Assert.notNull(trackInfoResult, "声音信息对象结果集不存在");
        TrackInfo trackInfo = trackInfoResult.getData();
        Assert.notNull(trackInfo, "声音信息对象不存在");
        // 查询用户购买过的声音
        List<UserPaidTrack> userPaidTrackList = userPaidTrackMapper.selectList(new LambdaQueryWrapper<UserPaidTrack>().eq(UserPaidTrack::getUserId, userPaidRecordVo.getUserId())
                .eq(UserPaidTrack::getAlbumId, trackInfo.getAlbumId()));
        List<Long> userPaidTrackIdList = userPaidTrackList.stream().map(UserPaidTrack::getTrackId).collect(Collectors.toList());
        // 重复付款则退款 (一般推荐购买和回显的时候都正常的话不会发生，UI上没有付款标识)
        userPaidRecordVo.getItemIdList().forEach(trackId -> {
            if (!CollectionUtils.isEmpty(userPaidTrackList)) {
                // 退款(重复支付的)
                if (userPaidTrackIdList.contains(trackId)) {
                    // 发送退款消息;  trackId, userId, amount,消费 receiver 调用相关 api增加账户金额
//                        rabbitService.sendMessage(MqConst.EXCHANGE_CANCEL_ORDER, MqConst.ROUTING_CANCEL_ORDER, OneEntity);
                }
            }
            // 其他正常保存记录
            UserPaidTrack userPaidTrack = new UserPaidTrack();
            userPaidTrack.setUserId(userPaidRecordVo.getUserId());
            userPaidTrack.setAlbumId(trackInfo.getAlbumId());
            userPaidTrack.setTrackId(trackId);
            userPaidTrack.setOrderNo(userPaidRecordVo.getOrderNo());
            userPaidTrackMapper.insert(userPaidTrack);
        });
    }
}
